package com.happy.cat.project.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.happy.cat.common.utils.SecurityUtils;
import com.happy.cat.common.utils.StringUtils;
import com.happy.cat.common.utils.poi.ExcelUtil;
import com.happy.cat.framework.web.controller.BaseController;
import com.happy.cat.framework.web.domain.AjaxResult;
import com.happy.cat.framework.web.page.TableDataInfo;
import com.happy.cat.framework.aspectj.lang.annotation.Log;
import com.happy.cat.framework.aspectj.lang.enums.BusinessType;
import com.happy.cat.project.system.domain.CatTeamCategory;
import com.happy.cat.project.system.domain.CatTeamCategoryItem;
import com.happy.cat.project.system.service.ICatTeamCategoryItemService;
import com.happy.cat.project.system.service.ICatTeamCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 组队品类Controller
 *
 * @author verne
 * @date 2020-07-18
 */
@AllArgsConstructor
@RestController
@RequestMapping("/system/teamCategoryItem" )
@Api("组队品类 API")
public class CatTeamCategoryItemController extends BaseController {

    private final ICatTeamCategoryItemService iCatTeamCategoryItemService;

    private final ICatTeamCategoryService iCatTeamCategoryService;

    @GetMapping(value = "/all" )
    public AjaxResult all() {
        List<CatTeamCategoryItem> list = iCatTeamCategoryItemService.list();
        if(list.size()<=0) {
            return AjaxResult.success(list);
        }

        Set<Long> ids = list.stream().map(CatTeamCategoryItem::getTeamCategoryId).collect(Collectors.toSet());
        List<CatTeamCategory> categorys = iCatTeamCategoryService.listByIds(ids);
        Map<Long, String> map = categorys.stream().collect(Collectors.toMap(CatTeamCategory::getId, CatTeamCategory::getName));

        list.forEach(e -> {
            e.setTeamCategoryName(map.get(e.getTeamCategoryId()));
        });
        return AjaxResult.success(list);
    }

    /**
     * 查询组队品类列表
     */
    @ApiOperation(value = "查询组队品类列表",notes = "查询组队品类列表",response = TableDataInfo.class)
    @PreAuthorize("@ss.hasPermi('system:teamCategoryItem:list')")
    @GetMapping("/list")
    public TableDataInfo list(CatTeamCategoryItem catTeamCategoryItem)
    {
        startPage();
        LambdaQueryWrapper<CatTeamCategoryItem> lqw = new LambdaQueryWrapper<CatTeamCategoryItem>();
        if (StringUtils.isNotBlank(catTeamCategoryItem.getName())){
            lqw.like(CatTeamCategoryItem::getName ,catTeamCategoryItem.getName());
        }
        if (catTeamCategoryItem.getTeamCategoryId() != null){
            lqw.eq(CatTeamCategoryItem::getTeamCategoryId ,catTeamCategoryItem.getTeamCategoryId());
        }
        if (StringUtils.isNotBlank(catTeamCategoryItem.getIcon())){
            lqw.eq(CatTeamCategoryItem::getIcon ,catTeamCategoryItem.getIcon());
        }
        if (StringUtils.isNotBlank(catTeamCategoryItem.getTags())){
            lqw.eq(CatTeamCategoryItem::getTags ,catTeamCategoryItem.getTags());
        }
        if (StringUtils.isNotBlank(catTeamCategoryItem.getTollType())){
            lqw.eq(CatTeamCategoryItem::getTollType ,catTeamCategoryItem.getTollType());
        }
        if (catTeamCategoryItem.getSortId() != null){
            lqw.eq(CatTeamCategoryItem::getSortId ,catTeamCategoryItem.getSortId());
        }
        if (catTeamCategoryItem.getCreateDate() != null){
            lqw.eq(CatTeamCategoryItem::getCreateDate ,catTeamCategoryItem.getCreateDate());
        }
        if (catTeamCategoryItem.getStatus() != null){
            lqw.eq(CatTeamCategoryItem::getStatus ,catTeamCategoryItem.getStatus());
        }
        List<CatTeamCategoryItem> list = iCatTeamCategoryItemService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出组队品类列表
     */
    @ApiOperation(value = "导出组队品类列表",notes = "导出组队品类列表",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:teamCategoryItem:export')" )
    @Log(title = "组队品类" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(CatTeamCategoryItem catTeamCategoryItem) {
        LambdaQueryWrapper<CatTeamCategoryItem> lqw = new LambdaQueryWrapper<CatTeamCategoryItem>(catTeamCategoryItem);
        List<CatTeamCategoryItem> list = iCatTeamCategoryItemService.list(lqw);
        ExcelUtil<CatTeamCategoryItem> util = new ExcelUtil<CatTeamCategoryItem>(CatTeamCategoryItem. class);
        return util.exportExcel(list, "teamCategoryItem" );
    }

    /**
     * 获取组队品类详细信息
     */
    @ApiOperation(value = "获取组队品类详细信息",notes = "获取组队品类详细信息",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:teamCategoryItem:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iCatTeamCategoryItemService.getById(id));
    }

    /**
     * 新增组队品类
     */
    @ApiOperation(value = "新增组队品类",notes = "新增组队品类",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:teamCategoryItem:add')" )
    @Log(title = "组队品类" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CatTeamCategoryItem catTeamCategoryItem) {
        catTeamCategoryItem.setCreateDate(new Date());
        catTeamCategoryItem.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iCatTeamCategoryItemService.save(catTeamCategoryItem) ? 1 : 0);
    }

    /**
     * 修改组队品类
     */
    @ApiOperation(value = "修改组队品类",notes = "修改组队品类",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:teamCategoryItem:edit')" )
    @Log(title = "组队品类" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CatTeamCategoryItem catTeamCategoryItem) {
        return toAjax(iCatTeamCategoryItemService.updateById(catTeamCategoryItem) ? 1 : 0);
    }

    /**
     * 删除组队品类
     */
    @ApiOperation(value = "删除组队品类",notes = "删除组队品类",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:teamCategoryItem:remove')" )
    @Log(title = "组队品类" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iCatTeamCategoryItemService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
